package com.baizan.daka.base

import android.view.View
import android.view.ViewGroup
import androidx.annotation.CallSuper
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.baizan.base.BaseFragment
import com.baizan.daka.widget.ListEmptyErrorView
import com.chad.library.adapter.base.BaseQuickAdapter
import com.dianzan.daka.R

abstract class BaseListFragment<P : BasePresenter, A : BaseQuickAdapter<*, *>> :
    BaseFragment<P>(), BaseQuickAdapter.OnItemClickListener {


    var adapter: A? = null


    protected var emptyErrorView: ListEmptyErrorView? = null

    @CallSuper
    override fun initData() {
        setUpRecycleView()
    }

    fun setUpRecycleView() {
        //adapter的包装类
        var mLinearLayoutManager = LinearLayoutManager(mContext)
        mLinearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL)
        mContext?.let {
            val recyclerView = it.findViewById<RecyclerView>(R.id.recycler_view_frg)
            recyclerView?.setLayoutManager(mLinearLayoutManager)
            adapter = initAdapter()
            recyclerView?.setAdapter(adapter)
            adapter?.setOnItemClickListener(this)
        }

    }


    open fun getDatasFail() {
        setListErrorView()
    }

    open fun <T> getDatasSuccess(response: List<T>?) {
        if (response == null) {
            setListEmptyView()
            return
        }
        adapter?.setNewData(response as List<Nothing>)
        setListEmptyView()
    }


    //初始化 列表数据的空状态
    protected open fun setListEmptyView() {
        addListEmptyErrorView()
        emptyErrorView?.setEmptyStatusing()
    }

    //初始化 列表数据的错误状态
    protected open fun setListErrorView() {
        addListEmptyErrorView()
        emptyErrorView?.setErrorStatusing()
    }

    open fun addListEmptyErrorView() {
        if (adapter?.emptyViewCount == 0) {
            if (emptyErrorView == null) {
                mContext?.let {
                    emptyErrorView = ListEmptyErrorView(it)
                }
                initListEmptyErrorData(emptyErrorView)
            }
//            emptyErrorView.setErrorClickListener(View.OnClickListener {
//                if (myRefreshLayout != null) {
//                    myRefreshLayout!!.autoRefresh()
//                } else {
//                    onRefresh()
//                }
//            })

            var vg = emptyErrorView?.parent
            vg?.let {
                var viewGroup = it as ViewGroup
                viewGroup.removeView(emptyErrorView)
            }
            adapter?.emptyView = emptyErrorView
        }
    }

    //默认的，特殊需要的重写
    protected open fun initListEmptyErrorData(view: ListEmptyErrorView?) {}


    override fun onItemClick(
        adapter: BaseQuickAdapter<*, *>?,
        view: View?,
        position: Int
    ) {

    }


    /**
     * 创建数据的Adapter
     * 要求实现自己的adapter
     */
    abstract fun initAdapter(): A?
    override fun layoutId(): Int = R.layout.act_base_list
}